/**
 * Created by XianTao on 2016/11/10
 * <p/>
 * Copyright (c) 2015-2015
 * Apache License
 * Version 2.0, January 2004
 * http://www.apache.org/licenses/
 */
package org.lego.wechat.base.api;

import org.lego.handle.exception.ExtraInvalidParameterException;
import org.lego.util.ApiResult;
import org.lego.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 获取微信服务器IP地址
 * <p/>
 * 如果公众号基于安全等考虑，需要获知微信服务器的IP地址列表，以便进行相关限制，可以通过该接口获得微信服务器IP地址列表或者IP网段信息。
 */
@Component
public class WechatIpApi {
    private String url = "https://api.weixin.qq.com/cgi-bin/getcallbackip";
    private String qyUrl = "";

    @Autowired
    private AccessTokenApi accessTokenApi;

    public List<String> getWechatIpList(String corpId, Integer agentId, String corpSecret) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("access_token", accessTokenApi.getWxAccessToken(corpId, agentId, corpSecret).getAccessToken());
        ApiResult apiResult = ApiResult.create(HttpUtil.get(url, params));
        if (!apiResult.isSucceed()) {
            throw new ExtraInvalidParameterException("WechatSystem.api.errorCode", String.valueOf(apiResult.getErrorCode()));
        }
        return apiResult.getList("ip_list");
    }
}
